package com.hmall.item.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.constatnts.MqConstants;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private IItemService itemService;


    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("{id}")
    Item selectById(@PathVariable("id") Long id){

        Item item = itemService.getById(id);
        return  item;
    }

    /**
     * 查询列表
     * @param page
     * @param size
     * @return
     */
    @ResponseBody
    @GetMapping("list")
//    @RequestMapping("/list")
    public PageDTO<Item> list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                            @RequestParam(value = "size", defaultValue = "5") Integer size){
//        return itemService.findAll(page,size);
        Page<Item> result = itemService.page(new Page<>(page, size));

        return new PageDTO<Item>(result.getTotal(), result.getRecords());
    }

    /**
     * 新增
     * @param item
     */
    @PostMapping("")
    void add(@RequestBody Item item){
        if (item.getId()!=null||"".equals(item.getId())){
            item.setUpdateTime(new Date());
            itemService.updateById(item);
            rabbitTemplate.convertAndSend(MqConstants.HMALL_EXCHANGE,MqConstants.HMALL_INSERT_KEY,item.getId());
            return;
        }
        itemService.save(item);
    }

    /**
     * 上架，下架
     * @param id
     * @param status
     */
    @PutMapping("/status/{id}/{status}")
    void putAndout(@PathVariable("id") Long id,@PathVariable("status") Integer status){
       itemService.putAndout(id,status);
        rabbitTemplate.convertAndSend(MqConstants.HMALL_EXCHANGE,MqConstants.HMALL_INSERT_KEY,id);
    }

    /**
     * 修改
     * @param item
     */
    @PutMapping("")
    void up(@RequestBody Item item){
        if (item.getId()!=null||"".equals(item.getId())){
            item.setUpdateTime(new Date());
            itemService.updateById(item);
            return;
        }
    }
    /**
     * 删除
     * @param id
     */
    @DeleteMapping("/{id}")
    void del(@PathVariable("id") Long id){
        itemService.removeById(id);
        rabbitTemplate.convertAndSend(MqConstants.HMALL_EXCHANGE,MqConstants.HMALL_DELETE_KEY,id);

    }


    /**
     * 根据id减库存
     * @param id
     * @param num
     * @return
     */
    @GetMapping("/downStock/{id}/{num}")
    public boolean downItemStockById(@PathVariable(name = "id") Long id,@PathVariable(name = "num") Integer num){
        boolean flag = itemService.downItemStockById(id,num);
        return flag;
    }

}
